package com.xyzh05.webapp.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.VipService;
import com.xyzh05.model.VipServiceVO;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.DateUtil;
import com.xyzh05.util.PageBean;

public class VipServiceAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = -3592051217851533012L;
	
	private VipServiceManager vipServiceManager;
	private List<VipService> vipServiceList;
	private VipService vipService;
	private VipServiceVO vipServiceVo;
	private PageBean pageBean;
	private Long id;
	private String query;
	private Integer resultCode;

	
    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }

    /**
     * 总编完成的任务
     * @return
     */
    public String toAccomplishTask(){
    	vipService = new VipService();
    	vipService.setEditor(userManager.get(id));
    	vipService.setStatus((short)1);
    	pageBean = vipServiceManager.findList(pageBean, vipService);
    	return SUCCESS;
    }
    
    /**
     * 总编未完成的任务
     * @return
     */
    public String toUndoneTask(){
    	vipService = new VipService();
    	vipService.setEditor(userManager.get(id));
    	vipService.setStatus((short)0);
    	pageBean = vipServiceManager.findList(pageBean, vipService);
    	return SUCCESS;
    }
    
    /**
     * 初始化所有任务
     * @return
     */
    public String list() {
    	vipService = new VipService();
    	if(getCurrentUser().isAdviserRole()){
    		vipService.setAdviser(getCurrentUser());
    	}else if(getCurrentUser().isEditorInChiefRole()){
    		vipService.setEditor(getCurrentUser());
    	}else if(getCurrentUser().isUserRole()){
    		vipService.setVip(getCurrentUser());
    	}
    	pageBean = vipServiceManager.findList(pageBean, vipService);
        return SUCCESS;
    }
    
    public String delete() {
        try {
        	vipService = vipServiceManager.get(id);
        	vipService.setStatus(VipService.STATUS_DISABLED);
        	vipServiceManager.saveOrUpdate(vipService);
    		resultCode = 1;
		} catch (Exception e) {
			resultCode = 0;
			log.warn(e.getMessage());
		}
		
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            vipService = vipServiceManager.get(id);
        } else {
            vipService = new VipService();
        }
        return SUCCESS;
    }

    public String save() throws Exception {  
    	vipService = new VipService();
    	try {
    		vipService.setVipServiceId(DateUtil.getDateTime("yyyyMMddHH", new Date()));
    		if(vipServiceVo.getVipId() != null){
    			vipService.setVip(userManager.get(vipServiceVo.getVipId()));
    		}
    		if(vipServiceVo.getEditorId() != null){
    			vipService.setEditor(userManager.get(vipServiceVo.getEditorId()));
    		}
    		vipService.setAdviser(getCurrentUser());
    		vipService.setPayment(vipServiceVo.getPayment());
    		vipService.setIspay(vipServiceVo.getIspay());
    		vipService.setPrice(vipServiceVo.getPrice());
    		vipService.setRepay(vipServiceVo.getRepay());
    		vipService.setStartTime(vipServiceVo.getStartTime());
    		vipService.setEndTime(vipServiceVo.getEndTime());
    		vipServiceManager.save(vipService);
    		resultCode = 1;
		} catch (Exception e) {
			resultCode = 0;
			log.warn(e.getMessage());
		}
    	return SUCCESS;
    }

    public String init(){
    	vipService = vipServiceManager.get(id);
    	return SUCCESS;
    }
    
	/**
	 * 更改留学顾问
	 * 
	 * @return
	 */
	public String toAdviser() {
		vipService = vipServiceManager.get(id);
		return SUCCESS;
	}
		
	public String doSaveAdviser() {
		VipService adviser = vipServiceManager.get(id);
		adviser.setAdviser(userManager.get(vipServiceVo.getId()));
		try {
			vipServiceManager.save(adviser);
			resultCode = 1;
		} catch (Exception e) {
			resultCode = 0;
			log.warn(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 更改主编
	 * 
	 * @return
	 */
	public String toEditor() {
		vipService = vipServiceManager.get(id);
		return SUCCESS;
	}
	
	public String doSaveEditor() {
		VipService editor = vipServiceManager.get(id);
		editor.setEditor(userManager.get(vipServiceVo.getId()));
		try {
			vipServiceManager.save(editor);
			resultCode = 1;
		} catch (Exception e) {
			resultCode = 0;
			log.warn(e.getMessage());
		}
		return SUCCESS;
	}
    
    
	public VipServiceManager getVipServiceManager() {
		return vipServiceManager;
	}

	public void setVipServiceManager(VipServiceManager vipServiceManager) {
		this.vipServiceManager = vipServiceManager;
	}	

	public List<VipService> getVipServiceList() {
		return vipServiceList;
	}

	public void setVipServiceList(List<VipService> vipServiceList) {
		this.vipServiceList = vipServiceList;
	}

	public VipService getVipService() {
		return vipService;
	}

	public void setVipService(VipService vipService) {
		this.vipService = vipService;
	}

	public VipServiceVO getVipServiceVo() {
		return vipServiceVo;
	}

	public void setVipServiceVo(VipServiceVO vipServiceVo) {
		this.vipServiceVo = vipServiceVo;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

}